package org.example.testWebsocket.redis;


import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.example.testWebsocket.entity.WsMsg;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class RedisListener implements MessageListener {
    @Resource
    private SimpMessagingTemplate simpMessagingTemplate;

    @Override
    public void onMessage(Message message, byte[] pattern) {
        log.info("onMessage,{}", message);
        String channel = new String(message.getChannel());
        log.info("channel:{}", channel);
        String messageContext = new String(message.getBody());
        log.info("消息内容： {}", messageContext);
        WsMsg msg = JSONUtil.toBean(messageContext, WsMsg.class);
        simpMessagingTemplate.convertAndSend(msg.getDestination(), msg.getMessageContent());
    }
}
